
package com.unifiedlibrary.amall_page.base.presenter;

import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import androidx.recyclerview.widget.RecyclerView;

import com.base.common.basemodule.BaseResponsceBean;
import com.base.common.basemodule.CheckNetWorkUtils;
import com.base.common.basemodule.MvpInstantiationUtils;
import com.base.common.widget.ForwardPopWindow;
import com.base.usefulModule.ToastUtil;
import com.unifiedlibrary.amall_page.R;
import com.unifiedlibrary.amall_page.base.model.BaseModel;
import com.unifiedlibrary.amall_page.base.view.BaseActivity;

/**
 * @author xzk
 * @data 2019/2/26
 * @email xiezhengkun@beyondsoft.com
 * @remark 基类presenter  MVP框架 Presenter基类  ， 负责数据的解析处理，关联model和view
 */
public class BasePresenter<M extends BaseModel, V extends BaseActivity> {
    protected M model;
    protected V context;

    public BasePresenter() {
    }

    /**
     * 绑定view和model
     *
     * @param context
     */
    public void  bindViewAndModel(V context) {
        this.context = context;
        model = MvpInstantiationUtils.getInstantiation(this, MvpInstantiationUtils.FIRSTGENERICITY);
        model.setPresenter(this);
    }

    /**
     * 回收所有绑定资源
     */
    public void recycleResource(){
        if(model != null){
            model.recycleResource();
            model = null;
        }
        if(context != null){
            context = null;
        }
    }

    protected void showLoadingDialog(){
        if(context == null){
            return;
        }
        context.showLoadingDialog();
    }
    protected void removeLoadingDialog(){
        if(context == null){
            return;
        }
        context.removeLoadingDialog();
    }
    /**
     * 请求异常提示
     */
    protected void showErrorMsg(){
        if(context == null){
            return;
        }
        context.removeLoadingDialog();
        context.closeRefresh();
        ToastUtil.showShort(context , R.string.str_system_error);
    }

    /**
     * 提示请求失败的提示语:土司方式
     * @param data
     */
    public void showFailMsg(BaseResponsceBean data){
        if(context == null){
            return;
        }
        context.removeLoadingDialog();
        context.closeRefresh();
        String msg = data.getMessage();
        if(TextUtils.isEmpty(msg)){
            msg = context.getString(R.string.str_system_error);
        }
        ToastUtil.showLong(context.getApplicationContext(),msg);
    }
    /**
     * 网络异常提示
     *   没有网，网络不可用
     * @return true 网络可用   false 网络不可用
     */
    protected boolean checkNetIsEnable(){
        if(!CheckNetWorkUtils.isNetworkConnected(context)){
            //网络不可用
            String msg = context.getString(R.string.str_network_not_available);
            ToastUtil.showShort(context, msg);
            return false;
        }
        if(CheckNetWorkUtils.getConnectedType(context)<0){
            //网络未连接
            String msg = context.getString(R.string.str_network_not_available);
            ToastUtil.showShort(context, msg);
            return false;
        }
        return true;
    }
    /**
     * 全屏的popwindow
     * 显示在某个view的下方全屏
     * @return
     */
    protected ForwardPopWindow showMatchPopWindow(View tagetView, View contentView){
        DisplayMetrics metrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        ForwardPopWindow mPopWindow = new ForwardPopWindow.PopupWindowBuilder(context).setView(contentView)
                                                                                       //显示大小
                                                                                       .size(
                                                                                           width,
                                                                                           ViewGroup.LayoutParams.WRAP_CONTENT)
                                                                                       //弹出popWindow时，背景是否变暗 这个属性设置后 ，会导致界面出现闪烁，不建议设置
                                                                                       .setTouchable(true)
                                                                                       .setOutsideTouchable(false)
                                                                                       .create();
        mPopWindow.showAsDropDown(tagetView, 0, 1, Gravity.BOTTOM);
        return mPopWindow;
    }
    /**
     * 全屏的popwindow
     * @return
     */
    protected ForwardPopWindow showMatchParentPopWindow(View tagetView, View contentView){
        //增加删除顶部状态栏，解决适配问题  注意适配所有弹窗
        DisplayMetrics metrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        height = height-context.getStatusBarHeight();
        ForwardPopWindow mPopWindow = new ForwardPopWindow.PopupWindowBuilder(context).setView(contentView)
                                                                                       //显示大小
                                                                                       .size(
                                                                                           width,
                                                                                           height)
                                                                                       //弹出popWindow时，背景是否变暗 这个属性设置后 ，会导致界面出现闪烁，不建议设置
                                                                                       .setTouchable(true)
                                                                                       .setOutsideTouchable(false)
                                                                                       .create();
        mPopWindow.showAtLocation(tagetView,  Gravity.BOTTOM, 0, 0);
        return mPopWindow;
    }
    /**
     * 自适应高度wrap  pop
     * @return
     */
    protected ForwardPopWindow showWrapParentPopWindow(View tagetView, View contentView){
        DisplayMetrics metrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        ForwardPopWindow mPopWindow = new ForwardPopWindow.PopupWindowBuilder(context).setView(contentView)
                                                                                       //显示大小
                                                                                       .size(
                                                                                           width,
                                                                                           ViewGroup.LayoutParams.WRAP_CONTENT)
                                                                                       //弹出popWindow时，背景是否变暗 这个属性设置后 ，会导致界面出现闪烁，不建议设置
                                                                                       .setTouchable(true)
                                                                                       .setOutsideTouchable(false)
                                                                                       .create();
        mPopWindow.showAtLocation(tagetView,  Gravity.BOTTOM, 0, 0);
        return mPopWindow;
    }
    /**
     * 弹窗提示,带黑背景的，适应宽高
     * 指定宽度
     */
    protected ForwardPopWindow showWrapPopWindowBack(View tagetView, View contentView, int width){
        ForwardPopWindow mPopWindow = new ForwardPopWindow.PopupWindowBuilder(context).setView(contentView)
                                                                                       //显示大小
                                                                                       .size(
                                                                                           width,
                                                                                           ViewGroup.LayoutParams.WRAP_CONTENT)
                                                                                       //弹出popWindow时，背景是否变暗 这个属性设置后 ，会导致界面出现闪烁，不建议设置
                                                                                       .enableBackgroundDark(true)
                                                                                       .setBgDarkAlpha(0.3f)
                                                                                       .setTouchable(true)
                                                                                       .setOutsideTouchable(false)
                                                                                       .create();
        mPopWindow.showAsDropDown(tagetView, 0, 10, Gravity.BOTTOM | Gravity.RIGHT);
        return mPopWindow;
    }
//    /**
//     * 当选项的数量超过8时，进行高度限制
//     *
//     * @param dataSize
//     * @param recyclerView
//     */
//    protected void limitRecycleViewHeight(int dataSize, RecyclerView recyclerView){
//        if(dataSize > 8){
//            ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
//            int[] screenSize = ScreenUtils.getScreenSize(context);
//            params.height=screenSize[1]/3;
//            recyclerView.setLayoutParams(params);
//        }
//    }

    /**
     * 现隐状态栏
     * @param enable
     */
    protected void full(boolean enable) {
        if (enable) {
            WindowManager.LayoutParams lp = context.getWindow().getAttributes();
            lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            context.getWindow().setAttributes(lp);
            context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else {
            WindowManager.LayoutParams attr = context.getWindow().getAttributes();
            attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            context.getWindow().setAttributes(attr);
            context.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }

}
